package com.medicine.hbp.ui;

import org.json.JSONException;
import org.json.JSONObject;
import org.kymjs.kjframe.http.HttpParams;
import org.kymjs.kjframe.ui.BindView;

import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.medicine.hbp.R;
import com.medicine.hbp.adapter.IntegralAdapter;
import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.http.AppHttpCallBack;
import com.medicine.hbp.http.AppHttps;
import com.medicine.hbp.http.HttpInterface;
import com.medicine.hbp.mob.findByListApplyListVo;
import com.medicine.hbp.mob.findIntegralActivityListVo;
import com.medicine.hbp.mob.findWalletByUserObjectVo;
import com.medicine.hbp.sp.LoginSp;
import com.medicine.hbp.uitl.StartIntent;
import com.medicine.hbp.uitl.ToastUtil;

/**
 *
 * @author 积分规则页面
 */
public class IntegralActivity extends BaseActivity {
	@BindView(id = R.id.listview)
	public ListView mListView;
	private IntegralAdapter adapter;
	@BindView(id = R.id.txt_jf)
	public TextView txt_jf;
	@BindView(id = R.id.txt_sjf)
	public TextView txt_sjf;
	@BindView(id = R.id.layout01)
	public LinearLayout layout01;
	@Override
	public void setRootView() {
		super.setRootView();
		setContentView(R.layout.activity_integral);
	}
	@Override
	public void initWidget() {
		super.initWidget();
		adapter = new IntegralAdapter(aty);
		mListView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		initTop();
		http();
		if(TextUtils.isEmpty(LoginSp.getToken(aty))){
			txt_sjf.setVisibility(View.VISIBLE);
			layout01.setVisibility(View.GONE);
			txt_sjf.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					StartIntent.startLogin(aty);
				}
			});
		}else{
			layout01.setVisibility(View.VISIBLE);
			txt_sjf.setVisibility(View.GONE);
			httpfinduserintegral();
		}

	}

	private void initTop() {
		TopView topview = (TopView) findViewById(R.id.topview);
		topview.setTitle("积分活动");
		topview.setLeftVisibility(View.GONE);
		topview.setLeftVisibilityExit(View.VISIBLE);
		topview.setRightVisibility(View.INVISIBLE);
		topview.setLeftOnClickExit(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}

	/**
	 *积分活动列表
	 */
	private void http() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(aty, HttpInterface.FIND_INTEGRAL_ACTIVITY, params,
				new AppHttpCallBack<findIntegralActivityListVo>(
						findIntegralActivityListVo.class, aty) {
					@Override
					public void onSuccess(findIntegralActivityListVo result) {
						adapter.addItems(result.info);
						adapter.notifyDataSetChanged();
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}

					@Override
					public void onFinish() {
						super.onFinish();
					}
				});

	}

	/**
	 * 获取活动积分数据接口
	 */
	private void httpfinduserintegral() {
		HttpParams params = new HttpParams();
		AppHttps.HttpPost(aty, HttpInterface.FIND_USER_INTEGRAL, params,
				new AppHttpCallBack<Object>(Object.class, aty) {
					@Override
					public void onSuccess(String t) {
						super.onSuccess(t);
						try {
							JSONObject object=new JSONObject(t);
							if(object.getBoolean("isSuccess")){
								int userIntegral=object.getInt("userIntegral");
								if(userIntegral<1){
									txt_sjf.setVisibility(View.VISIBLE);
									layout01.setVisibility(View.GONE);
									txt_sjf.setText("您当前没有积分");
								}else{
									txt_jf.setText(String.valueOf(object.getInt("userIntegral")));
									layout01.setVisibility(View.VISIBLE);
									txt_sjf.setVisibility(View.GONE);
								}

							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onSuccess(Object result) {
					}

					@Override
					public void onFailures(int errorCode, String errorMsg) {
						ToastUtil.showShort(aty, errorMsg);
					}
				});

	}
}
